﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Data;
using System.Text;
using Maticsoft.DBUtility;
using Utils;
using System.Collections.Specialized;
using Maticsoft.Model;


public partial class team_ask : FBasePage
{
    // Fields

    public Team m_team = null;
    protected NameValueCollection order = new NameValueCollection();
    public Maticsoft.BLL.Order orderbll = new Maticsoft.BLL.Order();
    public List<Order> orderlist = new List<Order>();

    public string strAbbreviation = "";
    public string strTeamID = "";
    public string strTeamTitle = "";
    public UserInfo userbll = new UserInfo();

    // Methods
    private void initList(string strTeamID)
    {
        int num;
        string where = "";
        if (base.ASSystem.Teamask == 0)
        {
            where = "Team_id=" + strTeamID;
        }
        else
        {
            where = "  Comment is not null ";
        }
        StringBuilder builder = new StringBuilder();
        DataSet set = DbHelperSQL.Page(10, this.pager.CurrentPageIndex, "ask", where, " Create_time desc", "*", out num);
        if (((set != null) && (set.Tables.Count > 0)) && (set.Tables[0].Rows.Count > 0))
        {
            for (int i = 0; i < set.Tables[0].Rows.Count; i++)
            {
                DataRow row = set.Tables[0].Rows[i];
                if ((row["Comment"] != null) && (row["Comment"].ToString() != ""))
                {
                    builder.Append("<li id=\"ask-entry-" + row["id"].ToString() + "\" >");
                    builder.Append("<div class=\"item\">");
                    UserInfo model = new Maticsoft.BLL.UserInfo().GetModel(int.Parse(row["User_id"].ToString()));
                    builder.Append("<p class=\"user\"><strong>" + model.Username + "</strong><span>" + DateTime.Parse(row["Create_time"].ToString()).ToString("yy-MM-dd") + "</span></p>");
                    builder.Append("<div class=\"clear\"></div>");
                    builder.Append("<p class=\"text\">" + row["Content"].ToString() + "</p>");
                    builder.Append("<p class=\"reply\"><strong>回复：</strong>" + row["Comment"] + "</p>");
                    builder.Append("</div>");
                    builder.Append("</li>");
                }
            }
        }
        this.ltAsk.Text = builder.ToString();
        this.pager.RecordCount = num;
    }

    private void initPage()
    {
        if ((base.Request["id"] == null) || (base.Request["id"].ToString() == ""))
        {
            base.Response.Redirect("index.aspx");
        }
        else if (Utility.IsNum(base.Request["id"].ToString()))
        {
            this.strTeamID = base.Request["id"].ToString();
        }
        else
        {
            base.SetSuccess("您输入的参数非法");
            base.Response.Redirect("/index.aspx");
            return;
        }
        if ((base.Request["type"] != null) && (base.Request["type"].ToString() != ""))
        {
            this.pask.Visible = false;
            this.ltMsg.Text = "<div id=\"consult-add-succ\" ><p>您的问题已成功提交，客服MM很快就会回复的，稍等一会儿再来看吧。</p><p><a href=\"/team.aspx?id=" + this.strTeamID + "\">返回本团购</a>，或<a id=\"consult-add-more\" href=\"ask.aspx?id=" + this.strTeamID + "\">还有其他问题？</a></p></div>";
        }
        this.m_team = new Maticsoft.BLL.Team().GetModel(int.Parse(this.strTeamID));
        if (this.m_team != null)
        {
            this.strTeamTitle = this.m_team.Title;
            this.team_id.Value = this.strTeamID;
        }
        this.initList(this.strTeamID);
    }

    private void initWebName()
    {
        Maticsoft.BLL.System system = new Maticsoft.BLL.System();
        string strWhere = "";
        DataSet list = system.GetList(strWhere);
        if (((list != null) && (list.Tables.Count > 0)) && (list.Tables[0].Rows.Count > 0))
        {
            if ((list.Tables[0].Rows[0]["abbreviation"] == null) || (list.Tables[0].Rows[0]["abbreviation"].ToString() == ""))
            {
                this.strAbbreviation = "艾尚团购";
            }
            else
            {
                this.strAbbreviation = list.Tables[0].Rows[0]["abbreviation"].ToString();
            }
        }
    }

    protected override void OnLoad(EventArgs e)
    {
        base.OnLoad(e);
        if (!this.Page.IsPostBack)
        {
            if (this.Page.User.Identity.Name == "")
            {
                this.pask.Visible = false;
                this.PLogin.Visible = true;
            }
            else
            {
                this.pask.Visible = true;
                this.PLogin.Visible = false;
            }
            this.initWebName();
            this.initPage();
        }
    }

    protected void pager_PageChanged(object sender, EventArgs e)
    {
        this.initPage();
    }

    protected void submit_ServerClick(object sender, EventArgs e)
    {
        if (this.consult_content.Value == "")
        {
            this.SetError("问题不能为空，请输入您的问题！");
            base.Response.Redirect("ask.aspx?id=" + this.team_id.Value);
        }
        else
        {
            Maticsoft.BLL.Ask ask = new Maticsoft.BLL.Ask();
            Ask model = new Ask();
            model.Team_id = int.Parse(this.team_id.Value);
            string name = this.Page.User.Identity.Name;
            Maticsoft.BLL.UserInfo info = new Maticsoft.BLL.UserInfo();
            string strWhere = "";
            strWhere = string.Format(" Username='{0}'", name);
            DataSet list = info.GetList(strWhere);
            if (((list != null) && (list.Tables.Count > 0)) && (list.Tables[0].Rows.Count > 0))
            {
                model.User_id = int.Parse(list.Tables[0].Rows[0]["id"].ToString());
            }
            model.Content = base.Server.HtmlEncode(this.consult_content.Value);
            if ((this.consult_content.Value.IndexOf("<") != -1) || (this.consult_content.Value.IndexOf(">") != -1))
            {
                this.SetError("输入非法字符，提交失败!");
                base.Response.Redirect("ask.aspx?id=" + this.team_id.Value);
            }
            else
            {
                model.Create_time = DateTime.Now;
                ask.Add(model);
                base.Response.Redirect("ask.aspx?id=" + this.team_id.Value + "&type=0");
            }
        }
    }

}